
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Random;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Russell
 */
public class Test {
    
    public int byteToInt(byte[] bytes, int size) {
        int result = 0;
        for (int i = 0; i < size; i++) {
            result = (result << 8) + (int) (bytes[i]&0xFF);
        }
        return result;
    }
    
     public int getRandom(){
        Random random = new Random();
        int temp = random.nextInt();
        return temp;
    }
    
     public static void main(String[] args) throws IOException, Exception {

        byte[] encryptionKey = "ac23%4,.Mfjke%~d".getBytes();
        byte[] integrityKey = "ajipog^^$#dd//.,".getBytes();
 	byte[] IV = "5jmN48(0".getBytes();
        
    
                File file = new File("cleartext.txt");
                InputStream is = new FileInputStream(file);
                if (is.available() > 0) {
                    System.out.println("Good to Go \n");
                } else {
                    System.out.println("fail \n");
                }
                int fileLength = (int) file.length();
                System.out.println("length = " + fileLength);
                byte[] fileBytes = new byte[fileLength];
                int offset = 0;
                int numRead = 0;

                numRead = is.read(fileBytes, offset, fileBytes.length - offset);
                offset += numRead;
                System.out.println("numRead = " + numRead + "\n");

                if (offset < fileBytes.length) {
                    throw new IOException("Could not completely read file " + file.getName());
                }
                is.close();
         
         
        EncryptClass rc1 = new EncryptClass("CBC", encryptionKey, integrityKey, IV, fileBytes, true);
        byte[] cipher = rc1.getCipher();
        EncryptClass rc2 = new EncryptClass("CBC", encryptionKey, integrityKey, IV, cipher, false);
        byte[] text = rc2.getText();
        String dec = new String(text, "UTF8");
        System.out.println("text = " + dec); 
        File newFile = new File("outputFile.txt");
        OutputStream os = new FileOutputStream(newFile);
        os.write(text);
        os.close();
     }   
    
    
}
